package com.doumi.biz.qadev.api.testcase.commonfilter;

import com.doumi.biz.qadev.api.methods.OperationRedis;
import com.doumi.biz.qadev.api.testcase.RequestTemplate;
import io.restassured.path.json.JsonPath;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by YDZ on 2019/11/29.
 */
public class CommonAdd {
    private RequestTemplate requestTemplate = new RequestTemplate();
    private OperationRedis operationRedis = new OperationRedis();
    private static List<String> cuIds = new ArrayList<>();
    HashMap hashMap = new HashMap();
    StringBuffer sb = new StringBuffer("[");
    //redis初始化数据
    @BeforeClass(description = "初始化redis数据,建用户信息初始化到redis并添加曝光")
    public  void operationData(){
        String filenanme = "src/source/redisfile/filter-username - 副本.csv";
        String mothodname = "businessbehavior";
        cuIds = operationRedis.readCsvoperationRedis(filenanme,mothodname,0);
        for (int i = 0; i <cuIds.size()-1;  i++) {
            sb.append(cuIds.get(i).trim()).append(",");
        }
        sb.append(cuIds.get(cuIds.size()-1).trim());
        sb.append("]");
    }
//    @Before
////            (description = "先添加曝光在进行过滤，理论应该过滤掉该用户")
//    public void  add(){
////        StringBuffer sb = new StringBuffer("[");
////        for (int i = 0; i <cuIds.size()-1;  i++) {
////            sb.append(cuIds.get(i).trim()).append(",");
////        }
////        sb.append(cuIds.get(cuIds.size()-1).trim());
////        sb.append("]");
//        hashMap.put("cuIds",sb.toString());
//        JsonPath js = requestTemplate.getHistoryAdd(hashMap);
//        String resultdata = js.getString("data[0]");
//        Assert.assertEquals(resultdata,"success");
//    }
    @Test(description = "将用户添加到企业曝光去，然后调用filter接口过滤，等待11s后逾期返回刚才添加的用户id")
    public void  checkHistoryAddAndSleep11SecondsCurlHistoryFilter(){
        hashMap.put("cuIds",sb.toString().trim());
        hashMap.put("businessType","1");
        hashMap.put("buserId","21318");
        hashMap.put("bUserDelayed","10");
        hashMap.put("bUserUnit","seconds");
        hashMap.put("cDelayed","10");
        hashMap.put("cUnit","seconds");
        hashMap.put("contactStatus",0);
        JsonPath jsadd = requestTemplate.getHistoryAdd(hashMap);
        String resultdata = jsadd.getString("data");
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        JsonPath js = requestTemplate.getHistoryFilter(hashMap);
        List resultList = js.getList("data");
        Assert.assertEquals(resultdata,"success");
        Assert.assertEquals(resultList.size(),cuIds.size());

    }
}
